package club.cearnach.api.security.config;

import club.cearnach.api.security.handler.EmailCodeAuthenticationFailureHandler;
import club.cearnach.api.security.handler.EmailCodeAuthenticationSuccessHandler;
import club.cearnach.core.property.VCodeProperties;
import club.cearnach.security.filter.EmailCodeAuthenticationFilter;
import club.cearnach.security.provider.impl.EmailCodeUserDetailsAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

/**
 * @author 阮胜
 * @date 2018/6/3 17:38
 */
@Component
public class EmailVerificationCodeSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
    @Autowired
    @Qualifier("emailCodeUserDetailService")
    private UserDetailsService userDetailsService;
    private final EmailCodeAuthenticationSuccessHandler emailCodeAuthenticationSuccessHandler;
    private final EmailCodeAuthenticationFailureHandler emailCodeAuthenticationFailureHandler;
    private final RedisTemplate<String, Object> redisTemplate;
    private final VCodeProperties vCodeProperties;

    public EmailVerificationCodeSecurityConfig(EmailCodeAuthenticationSuccessHandler emailCodeAuthenticationSuccessHandler, EmailCodeAuthenticationFailureHandler emailCodeAuthenticationFailureHandler, RedisTemplate<String, Object> redisTemplate, VCodeProperties vCodeProperties) {
        this.emailCodeAuthenticationSuccessHandler = emailCodeAuthenticationSuccessHandler;
        this.emailCodeAuthenticationFailureHandler = emailCodeAuthenticationFailureHandler;
        this.redisTemplate = redisTemplate;
        this.vCodeProperties = vCodeProperties;
    }


    @Override
    public void configure(HttpSecurity builder) {
        EmailCodeAuthenticationFilter emailCodeAuthenticationFilter = new EmailCodeAuthenticationFilter(redisTemplate, vCodeProperties);
        emailCodeAuthenticationFilter.setAuthenticationSuccessHandler(emailCodeAuthenticationSuccessHandler);
        emailCodeAuthenticationFilter.setAuthenticationFailureHandler(emailCodeAuthenticationFailureHandler);
        emailCodeAuthenticationFilter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
        EmailCodeUserDetailsAuthenticationProvider emailVerificationCodeAuthenticationProvider = new EmailCodeUserDetailsAuthenticationProvider();
        emailVerificationCodeAuthenticationProvider.setUserDetailsService(userDetailsService);
        builder.authenticationProvider(emailVerificationCodeAuthenticationProvider)
                .addFilterAfter(emailCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

    }
}
